<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/6/8
 * Time: 14:52
 */

namespace app\applet\controller;


use app\share\model\Token;
use think\Validate;

class Address
{
    public function __construct(){
        $this->address=new \app\applet\model\Address();
        $this->token=new Token();
    }

    /**
     * 收货地址列表
     */
    public function addressList(){
        $page=empty($input['current_page']) ? 1 : $input['current_page'];
        $page_size=empty($input['page_num']) ? 10: $input['page_num'];
        $member_id = get_header_id("token");

        $returnData=$this->address->addressList($page,$page_size,$member_id);

        returnJson(200,'success',$returnData);
    }

    /**
     * 添加收货地址    ---------修改  省市区 经纬度 ----------------
     */
    public function addressAdd(){
        $param=input();
        $validate = new Validate([
            ['m_name', 'require', '收货人不能为空'],
            ['m_mobile', 'require', '手机号不能为空'],
            ['province', 'require', '省不能为空'],
            ['city', 'require', '市不能为空'],
            ['county', 'require', '区不能为空'],
//            ['longitude', 'require', '经度不能为空'],
//            ['latitude', 'require', '纬度不能为空'],
            ['address_detail', 'require', '详细地址不能为空'],
            ['is_default', 'require', '设为默认地址状态不能为空'],
        ]);
        if(!$validate->check($param))
        {
            returnJson(-2003,$validate->getError());
        }
        $member_id = get_header_id("token");
        $param['member_id']=$member_id;
        $param['create_time']=time();
        $param['location_area']=$param['province'].$param['city'].$param['county'];
        $this->address->addressAdd($param);

        returnJson(200,'提交成功');

    }

    /**
     * 删除收货地址
     */
    public function addressDel(){
        $member_id = get_header_id("token");
        $id=input('ma_id');
        $this->address->addressDel($id,$member_id);

        returnJson(200,'删除成功');

    }

    /**
     * 设置默认地址
     */
    public function addressDefault(){
        $member_id = get_header_id("token");
        $id=input('ma_id');
        $this->address->addressSet($id,$member_id);
        returnJson(200,'设置成功');

    }

    /**
     * 编辑收货地址提交
     */
    public function addressEdit(){
        $member_id = get_header_id("token");

        $param=input();
        $validate = new Validate([
            ['m_name', 'require', '收货人不能为空'],
            ['m_mobile', 'require', '手机号不能为空'],
            ['province', 'require', '省不能为空'],
            ['city', 'require', '市不能为空'],
            ['county', 'require', '区不能为空'],
            ['longitude', 'require', '经度不能为空'],
            ['latitude', 'require', '纬度不能为空'],
            ['address_detail', 'require', '详细地址不能为空'],
            ['is_default', 'require', '设为默认地址状态不能为空'],
            ['ma_id', 'require', 'id不能为空'],
        ]);
        if(!$validate->check($param))
        {
            returnJson(-2003,$validate->getError());
        }
        $id=$param['ma_id'];
        $data['m_name']=$param['m_name'];
        $data['m_mobile']=$param['m_mobile'];
        $data['location_area']=$param['province'].$param['city'].$param['county'];
        $data['province']=$param['province'];
        $data['city']=$param['city'];
        $data['county']=$param['county'];
        $data['longitude']=$param['longitude'];
        $data['latitude']=$param['latitude'];
        $data['address_detail']=$param['address_detail'];
        $data['is_default']=$param['is_default'];

        $this->address->addressEdit($data,$id);

        returnJson(200,'提交成功');


    }

    /**
     * 编辑收货地址页面数据
     */
    public function address(){
        $member_id = get_header_id("token");
        $id=input('ma_id');

        $returnData=$this->address->addressOne($id);

        returnJson(200,'success',$returnData);

    }

}